package gameobject.construct;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.math.Vector3f;
import java.io.IOException;
import mygame.Vector3i;
import mygame.Voxel;
import mygame.VoxelGrid;

public abstract class BaseTree extends Construct {
    protected static final int DEFAULT_HEIGHT = 3;
    protected int height = DEFAULT_HEIGHT;

    @Override
    protected int getNumOccupiedVoxels() {
	return height;
    }
    
    @Override
    protected void setLocalTranslationWithRespectToBase() {
        Vector3f localTrans = VoxelGrid.getWorldLocation(baseVoxelGridLocation).clone();
        float shift = -Voxel.VOXEL_EXTENT;
        localTrans.addLocal(0f, shift, 0f);
        setLocalTranslation(localTrans);
    }

    @Override
    protected void recalculateOccupiedVoxels() {
	System.out.println("Recalculating occupied voxels...");
        Vector3i tempLocation = baseVoxelGridLocation.clone();
        for(Voxel occupiedVoxel : occupiedVoxels) {
            occupiedVoxel.setVoxelGridLocation(tempLocation);
            tempLocation.addY(1);
        }
        System.out.println(occupiedVoxels);
    }

    protected void setHeight(int height) {
        this.height = height;
    }
    
    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
	OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(height, "height", DEFAULT_HEIGHT);
    }
    
    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
	InputCapsule capsule = im.getCapsule(this);
        height = capsule.readInt("height", DEFAULT_HEIGHT);
    }
}
